package com.example.com.transcribeeditpublish.popuWindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.example.com.transcribeeditpublish.R;
import com.example.com.transcribeeditpublish.activity.PictureEditerActivity;

import java.util.ArrayList;

import me.goldze.mvvmhabit.utils.GlidRadioTranform;


/**
 * Created by i7-1 on 2018/12/12.
 */

public class FilterPpwindow extends PopupWindow {

    private final RecyclerView recyclerView;
    private final FilterCallBack listener;
    private final Context mContext;
    private ArrayList<Integer> mFilterList;

    public FilterPpwindow(PictureEditerActivity context, FilterCallBack filterCallBack) {
        this.mContext = context;
        final View mMenuView = LayoutInflater.from(context).inflate(R.layout.popuwindow_filter, null);
        recyclerView = (RecyclerView) mMenuView.findViewById(R.id.f_rv);
        this.listener = filterCallBack;
        setRecyclerView(context);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //适配虚拟键
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000); //android:background="#30000000"
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);

    }

    private void setRecyclerView(PictureEditerActivity context) {
        mFilterList = new ArrayList<Integer>();
        mFilterList.add(R.mipmap.orginal);
        mFilterList.add(R.mipmap.biaozhun);
        mFilterList.add(R.mipmap.yinghong);
        mFilterList.add(R.mipmap.yunshang);
        mFilterList.add(R.mipmap.chunzhen);
        mFilterList.add(R.mipmap.bailan);
        mFilterList.add(R.mipmap.yuanqi);
        mFilterList.add(R.mipmap.chaotuo);
        mFilterList.add(R.mipmap.xiangfen);

        mFilterList.add(R.mipmap.langman);
        mFilterList.add(R.mipmap.qingxin);
        mFilterList.add(R.mipmap.weimei);
        mFilterList.add(R.mipmap.fennen);
        mFilterList.add(R.mipmap.huaijiu);
        mFilterList.add(R.mipmap.landiao);
        mFilterList.add(R.mipmap.qingliang);
        mFilterList.add(R.mipmap.rixi);

        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

        recyclerView.setAdapter(new FAdapter());
    }

    private class FAdapter extends RecyclerView.Adapter<FilterViewHolder> {

        private int mCurrentSelectedPos;

        @Override
        public FilterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new FilterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.filter_layout, parent, false));
        }

        @Override
        public void onBindViewHolder(FilterViewHolder holder, final int position) {

            Glide.with(mContext).load(mFilterList.get(position)).transform(new GlidRadioTranform(mContext)).into(holder.ivImage);
            Glide.with(mContext).load(R.mipmap.filter_selected).transform(new GlidRadioTranform(mContext)).into(holder.ivImageTint);

            if (mCurrentSelectedPos == position) {
                holder.ivImageTint.setVisibility(View.VISIBLE);
            } else {
                holder.ivImageTint.setVisibility(View.GONE);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setCurrentSelectedPos(position);
                }
            });
        }

        public void setCurrentSelectedPos(int pos) {
            int tPos = mCurrentSelectedPos;
            mCurrentSelectedPos = pos;
            this.notifyItemChanged(tPos);
            this.notifyItemChanged(mCurrentSelectedPos);
            if (listener != null) {
                listener.callback(pos);
            }
        }

        @Override
        public int getItemCount() {
            return mFilterList.size();
        }
    }

    private class FilterViewHolder extends RecyclerView.ViewHolder {

        public final ImageView ivImage;
        public final ImageView ivImageTint;

        public FilterViewHolder(View inflate) {
            super(inflate);
            ivImage = (ImageView) itemView.findViewById(R.id.filter_image);
            ivImageTint = (ImageView) itemView.findViewById(R.id.filter_image_tint);
        }
    }

    public interface FilterCallBack {
        void callback(int position);
    }
}
